/**
 * [605] 种花问题
 *
 * @format
 * @lc app=leetcode.cn id=605 lang=javascript
 */

// @lc code=start
/**
 * @param {number[]} flowerbed
 * @param {number} n
 * @return {boolean}
 */
var canPlaceFlowers = function (flowerbed, n) {
    let i = 0,
        len = flowerbed.length
    while (i < len && n) {
        if (i == 0) !flowerbed[0] && !flowerbed[1] ? ((flowerbed[0] = 1), n--) : null
        else if (i == len - 1) !flowerbed[len - 2] && !flowerbed[len - 1] ? ((flowerbed[len - 1] = 1), n--) : null
        else !flowerbed[i - 1] && !flowerbed[i] && !flowerbed[i + 1] ? ((flowerbed[i] = 1), n--) : null
        i++
    }
    return !n
}

// var canPlaceFlowers = function (flowerbed, n) {
//     const max = Math.floor(len / 2) + (len % 2)
//     return n <= max
// }
// @lc code=end
debugger
console.log(canPlaceFlowers([1,0,0,0,1],2))